//
//  LGTHomeVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/7.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import ZHRefresh

class LGTHomeVC: LGTBaseVC, UITableViewDelegate, UITableViewDataSource{
    var tableView:UITableView?
    var headView:LGTHomeHeaderView?
    var data:Array<TaskModel> = []
    var page:Int = 1
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    override func setUI() {
        super.setUI()
        self.setBavBarStyle(navBarHiden: false, title: "", back: false)
        let appNameView:UIImageView = LGTViewCreateUtil.share.createImageView(image: "app_name_logo")
        self.navBar.addSubview(appNameView)
        appNameView.snp.makeConstraints { (make) in
            make.width.equalTo(61)
            make.height.equalTo(22)
            make.left.equalTo(SizeFit(9))
            make.top.equalTo(36 + IPHONENAV_DIS)
        }
        
        let searchImageView:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(13))
        searchImageView.backgroundColor = ViewBackColor
        self.navBar.addSubview(searchImageView)
        searchImageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(appNameView)
            make.height.equalTo(26)
            make.left.equalTo(78)
            make.right.equalTo(-8)
        }
        searchImageView.isUserInteractionEnabled = true
        let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(searchAction))
        searchImageView.addGestureRecognizer(tap)
        
        let searchIcon:UIImageView = LGTViewCreateUtil.share.createImageView(image: "search")
        searchImageView.addSubview(searchIcon)
        searchIcon.snp.makeConstraints { (make) in
            make.width.height.equalTo(12)
            make.left.equalTo(21)
            make.centerY.equalToSuperview()
        }

        let searchLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: GrayColor, fontSize: CGFloat(12), text: "任务标签、任务ID")
        searchImageView.addSubview(searchLabel)
        searchLabel.snp.makeConstraints { (make) in
            make.height.equalTo(12)
            make.left.equalTo(38)
            make.centerY.equalToSuperview()
        }
        
        
        self.tableView = UITableView.init(frame: CGRect.init(x: 0.0, y: NavBarH, width: Double(KScreenW), height: Double(KScreenH) - TabBarH - NavBarH), style: UITableView.Style.plain)
        self.view.addSubview(self.tableView!)
        self.tableView?.backgroundColor = ViewBackColor
        self.tableView?.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.register(LGTHomeCell.classForCoder(), forCellReuseIdentifier: "cell")
        self.headView = LGTHomeHeaderView.init(reuseIdentifier: "head")
        self.headView?.frame = CGRect.init(x: 0, y: 0, width: Double(KScreenW), height: SizeFit(257))
         self.tableView?.tableHeaderView = self.headView
        
        weak var weakSelf = self
        self.tableView?.header = ZHRefreshHeader.headerWithRefreshing(block: {
            weakSelf?.page = 1
            weakSelf?.tableView?.footer?.endRefreshing()
            weakSelf?.getData()
            weakSelf?.getBanner()
        })
        
        self.tableView?.footer = ZHRefreshBackNormalFooter.footerWithRefreshing(block: {
            weakSelf?.page += 1
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.getData()
        })
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:LGTHomeCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LGTHomeCell
        cell.model = self.data[indexPath.row]
        return cell
    }
    
    func getData(){
        weak var weakSelf = self
        LGTHttpUtil.httpUtil.requestData(url: LLZ_Task_List_URL, param: ["page": self.page], showMessage: false, success: { (json) in
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.tableView?.footer?.endRefreshing()
            let data:Array<Dictionary<String, Any>> =  json["data"] as? Array<Dictionary<String, Any>> ?? Array.init()
            var modeArray:Array<TaskModel> = Array.init()
            for i in 0..<data.count{
                let param:Dictionary<String, Any> = data[i]
                let model:TaskModel = try! lgtDecode(TaskModel.self, param: param)
                modeArray.append(model)
            }
            
            if weakSelf?.page == 1{
                weakSelf?.data = modeArray

            }else{
                if weakSelf?.data == nil{
                    weakSelf?.data = Array.init()
                }
                weakSelf?.data.append(contentsOf: modeArray)
            }
            
            if modeArray.count == 0{
                weakSelf?.tableView?.footer?.endRefreshingWithNoMoreData()
            }else{
                weakSelf?.tableView?.footer?.resetNoMoreData()
            }
            weakSelf?.tableView?.reloadData()
        }, failure: { (json) in
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.tableView?.footer?.endRefreshing()
        })
    }
    
    
    //banner
    func getBanner(){
//        LGTHttpUtil.httpUtil.requestData(url: Banner_URL, param: ["type": 1], success: { (json) in
//            
//            // print("")
//            
//        }, failure: { (json) in
//            
//        })
    }
    
    @objc func searchAction(){
        let searchVC:LGTHomeSearchVC = LGTHomeSearchVC()
        self.navigationController?.pushViewController(searchVC, animated: true)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if self.data.count == 0{
            self.tableView?.header?.beginRefreshing()
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let detailVC:LGTPostDetailVC = LGTPostDetailVC()
        detailVC.viewDetailType = DetailType.LLZ_Task_Detail_Type
        //detailVC.isPreview = false
        detailVC.taskModel = self.data[indexPath.row]
        //detailVC.isRWZ = false
        self.navigationController?.pushViewController(detailVC, animated: true)
    }
}
